package com.soul.doc.controller;

import cn.hutool.core.util.ObjectUtil;
import com.soul.common.bean.vo.UserVo;
import com.soul.common.msg.ApiResult;
import com.soul.doc.bean.dto.UploadFileDto;
import com.soul.common.bean.vo.FileVo;
import com.soul.doc.bean.po.FilePo;
import com.soul.doc.service.FileService;
import com.soul.doc.utils.MinioUtil;
import io.minio.GetObjectArgs;
import io.minio.GetObjectResponse;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.soul.common.controller.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sws
 * @since 2023-11-19 11:05:20
 */
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

    @Autowired
    private FileService fileService;
    @Autowired
    private MinioUtil minioUtil;
    private String bucketName = "files";
    @PostMapping("/upload")
    private ApiResult<FileVo> uploadFile(UploadFileDto fileDto){
        FileVo fileVo = fileService.uploadFile(fileDto);
        return ApiResult.success(fileVo);
    }

    @PostMapping("/query/ids")
    public ApiResult<List<FileVo>> queryUserVoByIds(@RequestBody Collection<Long> ids){
        List<FileVo> userVos = this.fileService.queryFileByIds(ids);
        return ApiResult.success(userVos);
    }

    @GetMapping("/music/{fileId}")
    public void getMusicFile(HttpServletResponse res,@PathVariable("fileId") Long fileId){
        fileService.getMusicFile(res,fileId);
    }
//    @GetMapping("/read/{fileId}")
//    public ResponseEntity<InputStreamResource> readFile(@PathVariable("fileId") Long fileId) {
//        try {
//            FilePo filePo = this.fileService.getById(fileId);
//            if (ObjectUtil.isEmpty(filePo)){
//                return ResponseEntity.notFound().build();
//            }
//            return minioUtil.getFile(bucketName,filePo.getFolderName(),filePo.getFileName());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResponseEntity.notFound().build();
//        }
//    }

    @PostMapping("/{fileId}/download")
    public void downloadFile(HttpServletResponse response,@PathVariable("fileId") Long fileId) throws UnsupportedEncodingException {
        FileVo fileVo = this.fileService.getFileVo(fileId);
        response.setCharacterEncoding("utf-8");
        // 设置强制下载不打开
        // res.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;fileName=".concat(URLEncoder.encode(fileVo.getFileName(), "UTF-8")));

        minioUtil.download(fileVo.getBucketName(),fileVo.getFolderName(),fileVo.getFileName(),response);
    }
}
